﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using VkClientIM.Converters;
using VkClientIM.Logic;
using Vkontakte.Api;
using Vkontakte.Api.Operations;

namespace VkClientIM.Windows
{
	/// <summary>
	/// Interaction logic for ProfileWindow.xaml
	/// </summary>
	public partial class ProfileWindow : Window
	{
		private Profile _user;
		
		ManualResetEventSlim _event=new ManualResetEventSlim(false);
		private ImageSource _image;
		public ProfileWindow()
		{
			InitializeComponent();
		}

		public void LoadProfile(Profile profile)
		{
			_user = profile;
			firstName.Text = profile.FirstName;
			lastName.Text = profile.LastName;
			nickName.Text = profile.NickName;
			activity.Text = profile.Activity;

			ProfilesGet getProfileset=new ProfilesGet();
			getProfileset.Uids.Add(_user.UserID);
			getProfileset.PersonProperties = PersonProperties.All;
			getProfileset.Completed += delegate
				{
					Dispatcher.Invoke(
						new Action( delegate
							{
								Person[] persons = getProfileset.GetResult();
								if ( ( persons != null ) && ( persons.Length != 0 ) )
								{
									Person p = persons[ 0 ];
									switch(p.Sex)
									{
										case Sex.None:
											sex.Text = "Неизвестный";
											break;
										case Sex.Male:
											sex.Text = "мужской";
											break;
										case Sex.Female:
											sex.Text="женский";
											break;
									}
									
									this.Title = "Информация о пользователе (идёт загрузка фотографии)";
									ImageDownloader.DownloadImage( p.PhotoBig,
									                               ( a,
									                                 b) => Dispatcher.Invoke(new Action<bool,byte[]>(ImageLoaded), a, b ) );

								}
							} )
						);
				};
			App.Current.ApiClient.ExecuteHighPriorityQuerry(getProfileset);
		}

		private void ImageLoaded(bool ok, byte[] array)
		{
			ImageSource src =
				( ImageSource )
				ByteArrayToImageConverter.Instance.Convert( array,
				                                            typeof (ImageSource),
				                                            null,
				                                            CultureInfo.
				                                            	CurrentCulture );
			this.Title = "Информация о пользователе";
			_image = src;
			_event.Set();
		}

		public ImageSource Image
		{
			get
			{
				_event.Wait();
				return _image;
			}
		}

		private void GetAudioMouseUp(object sender, MouseButtonEventArgs e)
		{
			AudioGet audioGet = new AudioGet();
			audioGet.UserID = _user.UserID;
			audioGet.Completed += delegate
			                      	{

			                      	};
			App.Current.ApiClient.ExecuteHighPriorityQuerry(audioGet);
		}

		private void GetVideoMouseUp(object sender, MouseButtonEventArgs e)
		{
			VideosWindow window=new VideosWindow();
			window.AttachProfile(_user);
			window.Show();
		}
	}
}
